package com.qinggeng.project.system.service.impl;

import com.google.common.collect.Maps;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.AppVersionDto;
import com.qinggeng.project.system.domain.AppVersion;
import com.qinggeng.project.system.mapper.AppVersionMapper;
import com.qinggeng.project.system.service.AppVersionService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Map;

@Service
public class AppVersionServiceImpl implements AppVersionService {

    @Resource
    private AppVersionMapper appVersionMapper;



    @Override
    public AppVersionDto selectAppVersionDtoByType(String channelType) {
        return appVersionMapper.selectAppVersionDtoByType(channelType);
    }

    @Override
    public AppVersion selectAppVersionByType(String channelType) {
        return appVersionMapper.selectAppVersionByType(channelType);
    }

    @Override
    public int updateAppVersion(AppVersion appVersion) {

        return appVersionMapper.updateAppVersion(appVersion);
    }


    @Override
    public boolean compareVersion(String version, String newVersion) {
        int i=0,j=0,versionNo=0,newVersionNo=0;
        do {
            //获取当前版本号“.”之前的数字
            while (i<version.length()){
                char charAt=version.charAt(i++);
                if(Character.isDigit(charAt)){
                    int num= Integer.parseInt(String.valueOf(charAt));
                    versionNo=versionNo*10+num;
                }else if(charAt=='.'){
                    break;
                }
            }

            //获取最新版本号“.”之前的数字
            while (j<newVersion.length()){
                char charAt=newVersion.charAt(j++);
                if(Character.isDigit(charAt)){
                    int num= Integer.parseInt(String.valueOf(charAt));
                    newVersionNo=newVersionNo*10+num;
                }else if(charAt=='.'){
                    break;
                }
            }
            if(versionNo<newVersionNo){
                return true;
            }else if(versionNo==newVersionNo){
                versionNo=0;newVersionNo=0;
            }else {
                break;
            }
        } while (i<version.length()||j<newVersion.length());
        return false;
    }
}
